package com.ctg.payroll2.action.sysadmin;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.bean.po.PbSecgroup;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.service.sysadmin.ResourceManService;
import com.ctg.payroll2.util.Constants;
import com.ctg.payroll2.util.PayrollUtil;
import com.ctg.platform.core.security.SpringSecurityManager;
import com.ctg.platform.core.security.entity.Resource;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 
 * @author 鲁力 2009-8-30 下午05:19:39
 *
 */
public class ResourceManAction extends ActionSupport {
	private static final long serialVersionUID = -5206127370425381528L;
	private SpringSecurityManager springSecurityManager;
	private ResourceManService resourceManService;
	private PbSecgroup node;
	private Resource res;
	private String msg = "";
	private String resIds;
	private String nodeId;
	public String checkNodeByName(){
		nodeId = resourceManService.getNodeByNodeName(node.getGroupName());
		return SUCCESS;
	}
	
	/**
	 * 新增资源
	 * @return 返回标记
	 */
	public String addRes(){
		SysUser sysUser= (SysUser)ServletActionContext.getRequest().getSession().getAttribute(Constants.CURRENT_USER);
		String resId =springSecurityManager.createSysResource(res, sysUser);
		if(!StringUtils.isEmpty(resId))
			resourceManService.adResId2PbSeckey(resId, node.getGroupCode());
		return SUCCESS;
	}
	/**
	 * 更新资源
	 * @return 返回标记
	 */
	public String updateRes(){
		springSecurityManager.updateResource(res);
		return SUCCESS;
	}
	/**
	 * 复查资源
	 * @return 返回标记
	 */
	public String retrieveRes(){
		String[] str= {res.getId()};
		List<Resource> list = springSecurityManager.getPrivilegesByPrivilegeIds(str);
		if (list.size()==1) {
			res = list.get(0);
		}
		String s = res.getDisabled();
		if(!s.equals("")){
			s=s.equals("1")?"冻结":"启用";
			res.setDisabled(s);
		}
		s=res.getType();
		if(!s.equals("")){
			if(s.equals("0"))
				res.setType("URL");
			else if(s.equals("1"))
				res.setType("按钮");
			else
				res.setType("其他");
		}
		return SUCCESS;
	}
	/**
	 * 删除资源
	 * @return 返回标记
	 */
	public String deleteResByResId(){
		SysUser sysUser= (SysUser)ServletActionContext.getRequest().getSession().getAttribute(Constants.CURRENT_USER);
		String[] array = PayrollUtil.splitStringToStringArray(resIds);
		springSecurityManager.deletePrivilegesById(array, sysUser);
		resourceManService.deleteSeckeyByResIds(array);
		return SUCCESS;
	}
	/**
	 * 新增结点
	 * @return 返回标记
	 */
	public String addNode(){
		resourceManService.addNode(node);
		return SUCCESS;
	}
	/**
	 * 更新结点
	 * @return 返回标记
	 */
	public String updateNode(){
		resourceManService.updateNode(node);
		return SUCCESS;
	}
	/**
	 * 复查结点
	 * @return 返回标记
	 */
	public String retrieveNode(){
		node = resourceManService.retrieveNode(node.getGroupCode());
		return SUCCESS;
	}
	/**
	 * 删除结点
	 * @return 返回标记
	 */
	public String deleteNode(){
		//先要判断其下是否有结点,有结点就不许删除
		int count = resourceManService.getChildrenCountByParentId(node.getGroupCode());
		msg = "";
		if (count>0) {
			msg = count+"";
			return SUCCESS;
		}
		resourceManService.deleteNodeByNodeId(node.getGroupCode());
		return SUCCESS;
	}
	/**********************************************
	 * 
	 * get, set 方法
	 * 
	 *********************************************/
	/**
	 * @return 返回res
	 */
	public Resource getRes() {
		return res;
	}
	/**
	 * @param res 设置res
	 */
	public void setRes(Resource res) {
		this.res = res;
	}
	/**
	 * @return 返回node
	 */
	public PbSecgroup getNode() {
		return node;
	}
	/**
	 * @param node 设置node
	 */
	public void setNode(PbSecgroup node) {
		this.node = node;
	}
	/**
	 * @param springSecurityManager 设置springSecurityManager
	 */
	public void setSpringSecurityManager(SpringSecurityManager springSecurityManager) {
		this.springSecurityManager = springSecurityManager;
	}
	/**
	 * @param resourceManService 设置resourceManService
	 */
	public void setResourceManService(ResourceManService resourceManService) {
		this.resourceManService = resourceManService;
	}
	/**
	 * @return 返回msg
	 */
	public String getMsg() {
		return msg;
	}
	/**
	 * @param msg 设置msg
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}
	/**
	 * @return 返回resIds
	 */
	public String getResIds() {
		return resIds;
	}
	/**
	 * @param resIds 设置resIds
	 */
	public void setResIds(String resIds) {
		this.resIds = resIds;
	}

	/**
	 * @return 返回nodeId
	 */
	public String getNodeId() {
		return nodeId;
	}

	/**
	 * @param nodeId 设置nodeId
	 */
	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}
}
